<template>
  <div class='my-container'>
    <!-- 未登录页面 -->
    <div class="header no-login" v-if="!user">
      <div class="login-btn" @click="$router.push('/login')">
        <img src="~@/assets/mobile.png" alt="">
        <span class="text">登录 / 注册</span>
      </div>
    </div>
    <!-- 已登录页面 -->
    <div class="header user-info" v-else>
      <div class="base-info">
        <div class="left">
          <van-image :src="userInfo.photo" fit="cover" round class="avator" />
          <span class="text">{{userInfo.name}}</span>
        </div>
        <div class="right">
          <van-button size="small" round to="/user/profile">编辑资料</van-button>
        </div>
      </div>
      <div class="data-dats">
        <div class="data-item">
          <div class="count">{{userInfo.art_count}}</div>
          <div class="text">头条</div>
        </div>
        <div class="data-item">
          <div class="count">{{userInfo.follow_count}}</div>
          <div class="text">关注</div>
        </div>
        <div class="data-item">
          <div class="count">{{userInfo.fans_count}}</div>
          <div class="text">粉丝</div>
        </div>
        <div class="data-item">
          <div class="count">{{userInfo.like_count}}</div>
          <div class="text">获赞</div>
        </div>
      </div>
    </div>
    <!-- 收藏和历史 -->
    <van-grid :column-num="2" clickable>
      <van-grid-item>
        <i slot="icon" class="iconfont icon-shoucang"></i>
        <span slot="text" class="text">收藏</span>
      </van-grid-item>
      <van-grid-item>
        <i slot="icon" class="iconfont icon-lishi"></i>
        <span slot="text" class="text">历史</span>
      </van-grid-item>
    </van-grid>
    <!-- 消息通知和小智同学 -->
    <van-cell-group>
      <van-cell title="消息通知" is-link to="/my" />
      <van-cell title="小智同学" is-link to="/my"/>
    </van-cell-group>
    <!-- 退出登录 -->
    <div class="exit-btn" v-if="user" @click="onLogout">
      <div>退出登录</div>
    </div>
  </div>
</template>
<script>
import { mapState } from 'vuex'
import { getUserInfo } from '@/api/user.js'
export default {
  name: 'MyPage',
  components: {},
  props: {},
  data() {
    return {
      userInfo: {}
    };
  },
  created() {
    // 当用户登录后 获取用户数据
    if(this.user) {
      this.getUserInfo()
    }
  },
  computed: {
    ...mapState(['user'])
  },
  methods: {
    onLogout() {
      this.$dialog.confirm({
        title: '是否确认退出?'
      })
        .then(() => {
          // on confirm
          this.$store.commit('setUser', null)
        })
        .catch(() => {
          // on cancel
        });
    },
    getUserInfo () {
      getUserInfo().then(res => {
        // console.log(res);
        this.userInfo = res
      }).catch(err => {
        console.log(err);
        this.$toast.fail(err || '获取用户信息失败')
      })
    }
  },
};
</script>
<style lang='less' scoped>
.my-container {
  .header {
    height: 361px;
    background: url('~@/assets/banner.png');
    background-size: cover;
  }
  .no-login {
    display: flex;
    justify-content: center;
    align-items: center;
    .login-btn {
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      > img {
        width: 132px;
        height: 132px;
        margin-bottom: 15px;
      }
      .text {
        font-size: 28px;
        color: #fff;
      }
    }
  }
  .user-info {
    .base-info {
      height: 231px;
      // background-color: pink;
      padding: 76px 32px 23px;
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .left {
        display: flex;
        align-items: center;
        .avator {
          width: 132px;
          height: 132px;
          border: 4px solid #fff;
        }
        .text {
          font-size: 29px;
          color: #fff;
          margin-left: 22px;
        }
      }
      .right {
        .van-button__text {
          color: #666;
        }
      }
    }
    .data-dats {
      // background-color: yellow;
      height: 130px;
      display: flex;
      color: #fff;
      .data-item {
        flex: 1;
        display: flex;
        justify-content: center;
        align-items: center;
        height: 130px;
        flex-direction: column;
        .count {
          font-size: 36px;
        }
        .text {
          font-size: 23px;
        }
      }
    }
  }
  .van-grid {
    // height: 141px;
    .van-grid-item {
      height: 141px;
      // border: 1px solid pink;
      .icon-shoucang {
        font-size: 54px;
        color: #eb5253;
      }
      .icon-lishi {
        font-size: 54px;
        color: #ff9d1d;
      }
      .text {
        font-size: 28px;
        color: #333;
      }
    }
  }
  .van-cell-group {
    margin: 9px 0;
  }
  .exit-btn {
    height: 104px;
    background-color: #fff;
    line-height: 104px;
    text-align: center;
    font-size: 30px;
    color: #d86262;
  }
}
</style>
